// 计划退入商品
var tblReturnStockInSkuPlanInfo;
// 实际退入商品
var tblReturnStockInSkuRealInfo;

// 页面初始化
$(function () {
    
    //默认基本信息显示
    showReturnStockInSkuPlanInfoTab();
    
    // tab切换事件
    $('a[data-toggle="tab"]').on('show.bs.tab', function (e) {
        
        switch ($(this).attr("id")) {
        case "returnStockInInfo":    //基本信息
            showReturnStockInInfoTab();
            break;
        case "returnStockInSkuPlanInfo":    //计划退入商品
        	showReturnStockInSkuPlanInfoTab();
            break;
        case "returnStockInSkuRealInfo":    //实际退入商品
            showReturnStockInSkuRealInfoTab();
            break;
        case "noteInfo":    //内部标签
            showNoteInfo();
            break;             
        default:
            break;
        }
    });
    
});

// 单价，金额格式化
var numberParser = function(data){
    var returnValue = "-";
    var numberData = parseFloat(data);
    if(numberData===+numberData){
        returnValue = numberData.toFixed(2);
    }
    return returnValue;
};

// 基本信息
function showReturnStockInInfoTab(){
    var stockinId = $('#stockinId', window.parent.document).val();

    $.ajax({
        type: "GET",
        url: "/returnStockin/getReturnStockInInfo/" + stockinId + ".action",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
//        async: false,//同步
        success: function (data) {
            if (data.result == "success") {
                $("#cons").text(data.data.cons); 
                $("#consTel").text(data.data.consTel); 
                $("#consMobile").text(data.data.consMobile); 
                $("#consAddr").text(data.data.consAddr); 
                $("#lgstName").text(data.data.lgstName); 
                $("#lgstNo").text(data.data.lgstNo); 
                $("#storName").text(data.data.storName); 
                if (data.data.factInTime != null) {
                    $("#factInTime").text(data.data.factInTime);
                }
                if (data.data.finishTime != null) {
                    $("#finishTime").text(data.data.finishTime);
                }
                $("#remark").text(data.data.remark);
            } else {
                window.top.window.showModalAlert(data.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            if (XMLHttpRequest.status == 500) {
                var result = eval("(" + XMLHttpRequest.responseText + ")");
                alert(result.errorObject.errorText);
            }
        }
    });
}

// 计划退入商品
function showReturnStockInSkuPlanInfoTab(){
    if (tblReturnStockInSkuPlanInfo!=null && tblReturnStockInSkuPlanInfo != undefined && ""!=tblReturnStockInSkuPlanInfo){
        tblReturnStockInSkuPlanInfo.draw();
        return;
    }
    
    var stockinId = $('#stockinId', window.parent.document).val();
    
    tblReturnStockInSkuPlanInfo = $('#tblReturnStockInSkuPlanInfo').DataTable({
        "bPaginate": false,
        "processing": true,
        "serverSide": true,
        "dom": '<"bottom"rtfli>',
        "searching": false,
        "deferRender": true,
        "info": false,
        "ajax": {
            "url": "/returnStockin/getReturnStockInSkuList.action",
            "data": function ( d ) {
                d.stockinId = encodeURI(stockinId);
                d.kind = encodeURI("plan");
            }
        },
        "tableTools": {
            "sRowSelect": "bootstrap"
        },
        "columns": [
        	{"data": "identification",
        		"render": function ( data, type, row, meta ) {
        			var hidInput = data +"<input type='hidden' value = '"+ row.exceptReason +"' />"
                    return hidInput;
                }
        	},
            {"data": "goodsCode"},
            {"data": "goodsName"},
            {"data": "skuCode"},
            {"data": "skuName"},
            {"data": "qty"},
            {"data": "factUnitPrice", "render": numberParser},
            {"data": "factUnitAmount", "render": numberParser},
            {"data": "divideGoodsFee", "render": numberParser},
            {"data": "divideGoodsAmount", "render": numberParser},
            {"data": "goodsSkuCost", "render": numberParser},
            {"data": "sn","visible":false}
        ],
        "columnDefs": [
        	{"className": "truncate-text tooltip-column-detail-5", "targets":[0]},
            {"className": "truncate-text tooltip-column-detail-5", "targets":[1]},
            {"className": "truncate-text tooltip-column-detail", "targets":[2]},
            {"className": "truncate-text tooltip-column-detail-5", "targets":[3]},
            {"className": "truncate-text tooltip-column-detail", "targets":[4]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[5]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[6]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[7]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[8]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[9]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[10]},
            {"className": "truncate-text tooltip-column-detail", "targets":[11]}
        ],
        "order": [[1, "asc"]]
    }).on('draw.dt', function () {
        // 如果是入库中，就显示SN号页面
        if(window.parent.window.$("li.active:eq(0)").find("a").attr("id")=="complete"){
            tblReturnStockInSkuPlanInfo.column(11).visible(true);
        }
    });
    
    // 文字过长截断显示悬浮框
    $("#tblReturnStockInSkuPlanInfo").tooltip({
        selector: ".tooltip-column-detail-5,.tooltip-column-detail",
        container: "body",
        title: function() {
        	if($(this)[0].children[1] == undefined){
        		if ($(this)[0].scrollWidth > $(this).outerWidth()) {
                    return $(this).text();
                }
        	}else{
        		return $(this)[0].children[1].value;
        	} 
        }
    });
}

// 实际退入商品
function showReturnStockInSkuRealInfoTab(){
    if (tblReturnStockInSkuRealInfo!=null && tblReturnStockInSkuRealInfo != undefined && ""!=tblReturnStockInSkuRealInfo){
        tblReturnStockInSkuRealInfo.draw();
        return;
    }
    
    var stockinId = $('#stockinId', window.parent.document).val();
    
    tblReturnStockInSkuRealInfo = $('#tblReturnStockInSkuRealInfo').DataTable({
        "bPaginate": false,
        "processing": true,
        "serverSide": true,
        "dom": '<"bottom"rtfli>',
        "searching": false,
        "deferRender": true,
        "info": false,
        "ajax": {
            "url": "/returnStockin/getReturnStockInSkuList.action",
            "data": function ( d ) {
                d.stockinId = encodeURI(stockinId);
                d.kind = encodeURI("real");
            }
        },
        "tableTools": {
            "sRowSelect": "bootstrap"
        },
        "columns": [
            {"data": "goodsCode"},
            {"data": "goodsName"},
            {"data": "skuCode"},
            {"data": "skuName"},
            {"data": "qty"},
            {"data": "inventoryType", 
                "render": function ( data, type, row, meta ) {
                    if (row.inventoryType == "zp") {
                        return "正品";
                    } else {
                        return "次品";
                    }
                }
            },
            {"data": "factUnitPrice", "render": numberParser},
            {"data": "factUnitAmount", "render": numberParser},
            {"data": "divideGoodsFee", "render": numberParser},
            {"data": "divideGoodsAmount", "render": numberParser},
            {"data": "goodsSkuCost", "render": numberParser},
            {"data": "sn","visible":false}
        ],
        "columnDefs": [
            {"className": "truncate-text tooltip-column-detail-5", "targets":[0]},
            {"className": "truncate-text tooltip-column-detail", "targets":[1]},
            {"className": "truncate-text tooltip-column-detail-5", "targets":[2]},
            {"className": "truncate-text tooltip-column-detail", "targets":[3]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[4]},
            {"className": "truncate-text tooltip-column-detail-5", "targets":[5]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[6]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[7]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[8]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[9]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[10]},
            {"className": "truncate-text tooltip-column-detail", "targets":[11]}
        ],
        "order": [[0, "asc"]]
    }).on('draw.dt', function () {
        // 如果是入库中，就显示SN号页面
        if(window.parent.window.$("li.active:eq(0)").find("a").attr("id")=="complete"){
            tblReturnStockInSkuRealInfo.column(11).visible(true);
        }
    });
    
    // 文字过长截断显示悬浮框
    $("#tblReturnStockInSkuRealInfo").tooltip({
        selector: ".tooltip-column-detail-5,.tooltip-column-detail",
        container: "body",
        title: function() {
            if ($(this)[0].scrollWidth > $(this).outerWidth()) {
                return $(this).text();
            }
        }
    });
}

// 内部标签
function showNoteInfo() {
    var stockinId = $('#stockinId', window.parent.document).val();
    var ifmSrc = "/views/base/note/notelist.html?busiKey=" + stockinId;
    $("#noteIframe").attr("src", ifmSrc);
}

